/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.datacenter.api.dto.*;
import org.cshoo.tattoo.datacenter.app.MessageAppService;
import org.cshoo.tattoo.datacenter.domain.po.Message;
import org.cshoo.tattoo.datacenter.domain.po.MessageItem;
import org.cshoo.tattoo.embed.infra.entity.PageQueryEntity;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Api(tags = "Message接口")
@RestController
@RequestMapping(value = "/message")
@Slf4j
public class MessageController {

    @Resource
    private MessageAppService messageAppService;

    /**
     * 判断编号是否存在
     *
     * @param code 编号
     * @return 判断结果
     */
    @ApiOperation("校验message编号")
    @RequestMapping(value = "/checkMessageCodeExists", method = RequestMethod.GET)
    public ValidResp checkMessageCodeExists(@RequestParam String code) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = messageAppService.checkMessageCodeExists(code);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加message
     *
     * @param addMessageReq 添加message request VO
     */
    @ApiOperation("添加message")
    @PostMapping(value = "/addMessage")
    public Response<?> addMessage(@RequestBody AddMessageReq addMessageReq) {
        log.info("addMessageReq:{}", addMessageReq);
        messageAppService.addMessage(addMessageReq);
        return Response.builder().build();
    }

    /**
     * 修改message
     *
     * @param updateMessageReq 修改message request
     */
    @ApiOperation("修改message")
    @PutMapping(value = "/updateMessage")
    public Response<?> updateMessage(@RequestBody @Valid UpdateMessageReq updateMessageReq) {
        log.info("updateMessageReq:{}", updateMessageReq);
        messageAppService.updateMessage(updateMessageReq);
        return Response.builder().build();
    }

    /**
     * 删除message
     *
     * @param id message id
     */
    @ApiOperation("删除message")
    @DeleteMapping(value = "/deleteMessage/{id}")
    public Response<?> deleteMessage(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        messageAppService.deleteMessage(id);
        return Response.builder().build();
    }

    /**
     * 查询message
     *
     * @return message 列表
     */
    @ApiOperation("查询message列表")
    @GetMapping(value = "/queryMessages")
    @Deprecated
    public List<Message> queryMessages() {
        return messageAppService.queryMessages();
    }

    /**
     * 分页查询message
     *
     * @param request 请求VO
     * @return 分页查询message对象
     */
    @ApiOperation("查询message列表")
    @PostMapping(value = "/page/queryMessages")
    public Page<Message> queryMessages(@RequestBody @Valid PageQueryEntity<String> request) {
        return messageAppService.queryMessages(request);
    }

    /**
     * 判断编号是否存在
     *
     * @param code     message项编号
     * @param messageId messageId
     * @return 判断结果
     */
    @ApiOperation("校验message项编号")
    @RequestMapping(value = "/checkMessageItemCodeExists", method = RequestMethod.GET)
    public ValidResp checkMessageItemCodeExists(@RequestParam String code, @RequestParam Long messageId) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = messageAppService.checkMessageItemCodeExists(code, messageId);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加message 项
     *
     * @param addMessageItemReq 添加message项VO
     */
    @ApiOperation("添加message项")
    @PostMapping(value = "/addMessageItem")
    public Response<?> addMessageItem(@RequestBody AddMessageItemReq addMessageItemReq) throws Exception {
        log.info("addMessageItemReq:{}", addMessageItemReq);
        messageAppService.addMessageItem(addMessageItemReq);
        return Response.builder().build();
    }

    /**
     * 修改message项
     *
     * @param updateMessageItemReq 修改message项VO
     */
    @ApiOperation("修改message项")
    @PutMapping(value = "/updateMessageItem")
    public Response<?> updateMessageItem(@RequestBody UpdateMessageItemReq updateMessageItemReq) {
        log.info("updateMessageItemReq:{}", updateMessageItemReq);
        messageAppService.updateMessageItem(updateMessageItemReq);
        return Response.builder().build();
    }

    /**
     * 删除message项
     *
     * @param id message项id
     */
    @ApiOperation("删除message项")
    @DeleteMapping(value = "/deleteMessageItem/{id}")
    public Response<?> deleteMessageItem(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        messageAppService.deleteMessageItem(id);
        return Response.builder().build();
    }

    /**
     * 查询message项
     *
     * @param messageId messageId
     * @return message项
     */
    @ApiOperation("查询message项")
    @GetMapping(value = "/queryMessageItems/{messageId}")
    public List<MessageItem> queryMessageItems(@PathVariable("messageId") Long messageId) {
        log.info("messageId:{}", messageId);
        return messageAppService.queryMessageItems(messageId);
    }

    /**
     * 查询message item的列表
     *
     * @param request 分页查询请求
     * @return message item 列表
     */
    @ApiOperation("查询message item列表")
    @PostMapping(value = "/page/queryMessageItems")
    public Page<MessageItem> queryMessageItems(@RequestBody @Valid PageQueryEntity<Long> request) {
        log.info("request:{}", request);
        return messageAppService.queryMessageItems(request);
    }

    /**
     * 查询单个message
     *
     * @param code 编号
     * @return message信息
     */
    @ApiOperation("查询单个的message")
    @GetMapping(value = "/querySingleMessage/{code}")
    public QuerySingleMessageResp querySingleMessage(@PathVariable("code") String code) {
        log.info("code:{}", code);
        return messageAppService.querySingleMessage(code);
    }
}